package com.usian.behavior.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.usian.behavior.entity.ApUnlikesBehavior;
import com.usian.behavior.mapper.ApUnlikesBehaviorMapper;
import com.usian.behavior.service.IApUnlikesBehaviorService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.usian.common.enums.AppHttpCodeEnum;
import com.usian.common.exception.LeadException;
import com.usian.common.threadlocal.UserThreadLocalUtils;
import com.usian.model.behavior.dtos.UnLikesBehaviorDto;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 * APP不喜欢行为表 服务实现类
 * </p>
 *
 * @author usian
 * @since 2022-07-18
 */
@Service
public class ApUnlikesBehaviorServiceImpl extends ServiceImpl<ApUnlikesBehaviorMapper, ApUnlikesBehavior> implements IApUnlikesBehaviorService {
    /**
     * 用户不喜欢行为
     * @param dto
     */
    @Override
    public void unLike(UnLikesBehaviorDto dto) {
        if (dto==null){
            throw new LeadException(AppHttpCodeEnum.PARAM_INVALID,"参数不正确");
        }

        //是否用户登录
        boolean isUser=false;
        //获取当前用户 id
        Integer userId = UserThreadLocalUtils.getUserId();
        //获取机械码
        String equipmentId = dto.getEquipmentId();
        if (userId==null && equipmentId==null){
            throw new LeadException(AppHttpCodeEnum.PARAM_INVALID,"机械码和用户id不能同时为空");
        }
        if (userId!=null && userId!=0){
            isUser=true;
        }
        //去查询这个不喜欢是否存在
        LambdaQueryWrapper<ApUnlikesBehavior> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ApUnlikesBehavior::getArticleId,dto.getArticleId());
        if (isUser){
            lambdaQueryWrapper.eq(ApUnlikesBehavior::getApUserId,userId);
        }else {
            lambdaQueryWrapper.eq(ApUnlikesBehavior::getEquipmentId,equipmentId);
        }

        ApUnlikesBehavior unlikesBehavior = getOne(lambdaQueryWrapper);
        //如果数据库中没有符合条件的数据说明是添加
        if (unlikesBehavior==null){
            unlikesBehavior = new ApUnlikesBehavior();
            unlikesBehavior.setCreatedTime(new Date());
            unlikesBehavior.setType(dto.getType());
            unlikesBehavior.setArticleId(dto.getArticleId());
            if (isUser){
                unlikesBehavior.setApUserId(userId);
            }else {
                unlikesBehavior.setEquipmentId(equipmentId);
            }
            save(unlikesBehavior);
        }else {
            //进行修改
            unlikesBehavior.setUpdateTime(new Date());
            unlikesBehavior.setType(dto.getType());
            updateById(unlikesBehavior);
        }

    }

    @Override
    public ApUnlikesBehavior findUnLikeByArticleId(Integer userId, String equipmentId, Long articleId) {
        if (userId==null&&equipmentId==null){
            throw new LeadException(AppHttpCodeEnum.PARAM_INVALID,"机械码和userid不能同时为空");
        }
        boolean isUser=false;
        if (userId!=null && userId!=0){
            isUser=true;
        }
        LambdaQueryWrapper<ApUnlikesBehavior> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ApUnlikesBehavior::getArticleId,articleId);
        if (isUser){
            lambdaQueryWrapper.eq(ApUnlikesBehavior::getApUserId,userId);
        }else {
            lambdaQueryWrapper.eq(ApUnlikesBehavior::getEquipmentId,equipmentId);
        }

        ApUnlikesBehavior apUnlikesBehavior = getOne(lambdaQueryWrapper);

        return apUnlikesBehavior;
    }
}
